public class num105 {
    private int index;
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return buildTreeNode(preorder,inorder,0,preorder.length - 1);
    }
    public TreeNode buildTreeNode(int[] preorder,int[] inorder,int left,int right){
        if(left > right){
            return null;
        }
        if(index >= preorder.length){
            return null;
        }
        //从前序遍历找节点
        TreeNode root = new TreeNode(preorder[index]);
        index++;
        int pos = find(inorder,root.val);
        root.left = buildTreeNode(preorder,inorder,left,pos - 1);
        root.right = buildTreeNode(preorder,inorder,pos + 1,right);
        return root;
    }
    public int find(int[] inorder,int val){
        for (int i = 0; i < inorder.length; i++) {
            if(inorder[i] == val){
                return  i;
            }
        }
        return -1;
    }
}
